﻿using System;
using System.Linq;
using System.Threading;
using Microsoft.Research.TouchMouseSensor;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using XMouseMSTM;

namespace XMouseMSTM
{
    class Program
    {

        static TouchMouseCallback myTouchMouseCallback;
        public static bool flagPressed = false;


        static void TouchMouseCallbackFunction(ref TOUCHMOUSESTATUS pTouchMouseStatus,
           byte[] pabImage,
           int dwImageSize)
        {
            /*
            // Iterate over rows.  (15(x) by 13(y) area)
            for (Int32 y = 0; y < pTouchMouseStatus.m_dwImageHeight; y++)
            {
                // Iterate over columns.
                for (Int32 x = 0; x < pTouchMouseStatus.m_dwImageWidth; x++)
                {
                    // Get the pixel value at current position.
                    int pixel = pabImage[pTouchMouseStatus.m_dwImageWidth * y + x];

                }
            }
             */
            if (pabImage[83] > 180)
            {
                if (flagPressed == false)
                {
                    WinAPI.keybd_event(0x1A, 0x1A, 0x0000 | 0, 0);
//                    WinAPI.keybd_event(0x5B, 0x5B, 0x0001 | 0x0002, 0);
                    flagPressed = true;
                }
            }
            else
            {
                if (flagPressed == true)
                {
                    WinAPI.keybd_event(0x1A, 0x1A, 0x0000 | 0x0002, 0);
//                    WinAPI.keybd_event(0x5B, 0x5B, 0x0001 | 0x0002, 0);
                    flagPressed = false;
                }
            }
            // This is the exact and only point i want to be my middle click and I want the strength of it be above 180 so there are no accidental clicks
        }






        static void Main(string[] args)
        {

            myTouchMouseCallback = new TouchMouseCallback(TouchMouseCallbackFunction);
            TouchMouseSensorInterop.RegisterTouchMouseCallback(myTouchMouseCallback);

            Application.Run();


            TouchMouseSensorInterop.UnregisterTouchMouseCallback();

        }
    }
}
